﻿using System;
using GrainManage.Base;
using GrainManage.Entities.Filter;
using GrainManage.Entities.Contact;
using GrainManage.Tools;
using GrainManage.Api;
using GrainManage.Proxy;
using System.Windows.Controls;

namespace GrainManage.Pages
{
    /// <summary>
    /// Interaction logic for Contact.xaml
    /// </summary>
    public partial class ContactPage : BasePage
    {
        #region 私有字段及构造函数

        public ContactPage()
        {
            InitializeComponent();

            RegisterService();

            BindQueryEvent();

            BindDropDownEvent();

            myPager.StartService(ContactApi.GetContactsWithPhoto);
        }

        #endregion

        #region 辅助函数

        private void RegisterService()
        {
            myPager.RegisterService((pageIndex, pageSize) =>
            {
                ContactFilter filter = GetContactFilter();
                TryBeginInvoke(() =>
                {
                    var input = new InputGetContactsWithPhoto()
                    {
                        Authentication = App.Authentication,
                        ContactFilter = filter,
                        PageIndex = pageIndex,
                        PageSize = pageSize
                    };
                    return Creator.ContactProxy.GetContactsWithPhoto(input);
                }, (result) =>
                {
                    UI.ShowContent(listview, result.Contacts);
                    myPager.TotalRowCount = result.Total;
                });
            }, ContactApi.GetContactsWithPhoto);
        }

        private void BindQueryEvent()
        {
            //*****************************************按键与双击事件*******************************************
            Action cbQueryAction = () => myPager.StartService(ContactApi.GetContactsWithPhoto);
            var eh = new GrainManage.Tools.EventHelper();
            eh.AddQueryEventHandler(cbName, cbQueryAction);
            eh.AddQueryEventHandler(cbArea, cbQueryAction);
            eh.AddQueryEventHandler(cbDetail, cbQueryAction);
            eh.AddQueryEventHandler(timeStart, cbQueryAction);
            eh.AddQueryEventHandler(timeEnd, cbQueryAction);
            eh.AddClickEventHandler(btnQuery, cbQueryAction);
        }

        private void BindDropDownEvent()
        {
            var eh = new GrainManage.Tools.EventHelper();
            //*****************************************填充联系人*******************************************
            eh.AddDropDownOpenedHandler(cbName, () =>
            {
                TryBeginInvoke(() =>
                {
                    return Creator.ContactProxy.GetContactName(PageSettings.ContactNameCount);
                }, (result) =>
                {
                    UI.ShowContent(cbName, result);
                });
            });

            //*****************************************填充地址*******************************************
            eh.AddDropDownOpenedHandler(cbArea, () =>
            {
                TryBeginInvoke(() =>
                {
                    return Creator.ContactProxy.GetContactArea(PageSettings.ContactAreaCount);
                }, (result) =>
                {
                    UI.ShowContent(cbArea, result);
                });
            });
        }

        private void BindContextEvent()
        {
            conMenu.PreviewMouseLeftButtonUp += (sender, e) =>
            {

            };
        }

        private ContactFilter GetContactFilter()
        {
            ContactFilter filter = new ContactFilter();
            filter.Name = cbName.Text.Trim(' ');
            filter.Area = cbArea.Text.Trim(' ');
            filter.Address = cbDetail.Text.Trim(' ');
            filter.StartTime = Convertor.ToDateTime(timeStart.Text.Trim(' '));
            filter.EndTime = Convertor.ToDateTime(timeEnd.Text.Trim(' '));
            return filter;
        }

        #endregion

        private void conMenu_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var cmd = (e.Source as TextBlock).Tag as string;
            switch (cmd)
            {
                case "Add":
               
                    break;
                case "Edit":
                    break;
                case "Delete":
                    break;
            }
        }

    }
}
